package com.bdqn.t350.controller;

import com.bdqn.t350.pojo.SysUser;
import com.bdqn.t350.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Resource
    SysUserService sysUserService;

    //@RequestMapping("/login")
    @PostMapping("/doLogin")
    public String doLogin(@RequestParam("userCode") String userCode,
                          @RequestParam("userPassword") String userPassword,
                          HttpServletRequest request) {
        System.out.println("login");
        SysUser user = new SysUser();
        user.setUserPassword(userPassword);
        user.setUserCode(userCode);
        SysUser sysUser = sysUserService.loginBiz(user);
        if (sysUser == null) {
            //重定向
            //return "redirect:http://www.baidu.com";
            //转发
            return "error";
        } else {
            //将登录的账号方到session中
            HttpSession session = request.getSession();
            session.setAttribute("sysUser", sysUser);
            //转发
            return "frame";
        }
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }


    @RequestMapping("/logOut")
    public String logOut(HttpServletRequest request) {
        //request.getSession().invalidate();
        request.getSession().removeAttribute("sysUser");
        return "redirect:login";
        // return "login";
    }
}
